//
//  WDVistorLoginView.swift
//  SINAWeiBo
//
//  Created by wangwei on 2016/11/13.
//  Copyright © 2016年 itcast. All rights reserved.
//
//   取消按钮就是注册, 确然按钮就是登录
import UIKit
import SnapKit

@objc protocol WDVistorLoginViewDelegate:NSObjectProtocol {

    
    //requested 不能被使用
    @objc optional func userWillLogin()
    @objc func userWillRegister()
    

}


class WDVistorLoginView: UIView {

    //声明属性代理,可选属性
    weak var delegate: WDVistorLoginViewDelegate?
    
    //现在是懒加载控件,一个圆圈,一个房子,还有遮罩,还有label,还有,button
    lazy var imgCircle = {  () -> UIImageView in
        let  image = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
        image.sizeToFit()
        return image
    }()
    lazy var imgCover = { () -> UIImageView in
        let  image = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
        image.sizeToFit()
        return image
    
    }()
    lazy var imgHouse = {  () -> UIImageView in
        let  image = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
        image.sizeToFit()
        return image
    }()
    lazy var label = { () -> UILabel in
    
        let lb = UILabel(title: "我还是一个小孩子,还是个懵懂的少年,没有什么", fontSize: 15, textColor: UIColor.darkGray)
        lb.preferredMaxLayoutWidth = 220
        lb.numberOfLines = 0
        lb.textAlignment = .center
        return lb
    }()
    
    lazy var cancelBtn = { () -> UIButton in
        
        let btn = UIButton(title: "取消", imageName: "common_button_white", textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1))
    
        return btn
    }()
    lazy var sureBtn = { () -> UIButton in
        
        let btn = UIButton(title: "确认", imageName: "common_button_white", textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1))
        
        return btn
    }()
    
    
    //当我们自定义view 的时候,系统我们应该重写frame 方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //MARK: 设置UI界面
        setupUI()
    }
    //这句代码非常重要,特别实在XIB 和Storyboard 的时候,非常重要,这样我们就可以实在本View 中添加XIB 了
    required init?(coder aDecoder: NSCoder) {
        // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    //外界设置界面信息的方法
    func updateInfo(title: String,imageName: String,isHome: Bool = false) {
        
        if !isHome {
            imgCircle.isHidden = true
        }
        imgHouse.image = UIImage(named: imageName)
        label.text = title
    }

    //设置UI界面
    func setupUI() {
        //MARK: 添加子控件
        addChildView()
        //MARK: 添加动画效果
        addAnimationDisplay()
        //MARK: 给按钮添加点击事件
        addClickBtn()
        
      
     
        
    }
    
   func addChildView() {
    //设置自己的颜色
    self.backgroundColor = UIColor.init(white: 223, alpha: 1)
    self.addSubview(imgCircle)
    self.addSubview(imgCover)
    self.addSubview(imgHouse)
    self.addSubview(label)
    self.addSubview(cancelBtn)
    self.addSubview(sureBtn)
    
    //约束imgCircle
    imgCircle.snp.makeConstraints { (make) in
        
        make.centerX.equalTo(self.snp.centerX)
        make.centerY.equalTo(self.snp.centerY).offset(-80)
    }
    //约束遮盖
    imgCover.snp.makeConstraints { (make) in
        make.centerX.equalTo(imgCircle)
        make.centerY.equalTo(imgCircle.snp.centerY)
    }
    
    //约束imgHouse
    imgHouse.snp.makeConstraints { (make) in
        
        make.centerX.equalTo(imgCircle.snp.centerX)
        make.centerY.equalTo(imgCircle.snp.centerY)
    }
    
    label.snp.makeConstraints { (make) in
        
        make.centerX.equalTo(imgCircle.snp.centerX)
        make.top.equalTo(imgCircle.snp.bottom).offset(20)
    }
    
    cancelBtn.snp.makeConstraints { (make) in
        
        make.trailing.equalTo(label.snp.centerX).offset(-20)
        make.top.equalTo(label.snp.bottom).offset(20)
        make.width.equalTo(80)
    }
    
    sureBtn.snp.makeConstraints { (make) in
        
        make.leading.equalTo(label.snp.centerX).offset(20)
        make.top.equalTo(label.snp.bottom).offset(20)
        make.width.equalTo(80)
    }
    
    }
   func addAnimationDisplay() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        animation.toValue = 2 * M_PI
        animation.isRemovedOnCompletion = false
        
        imgCircle.layer.add(animation, forKey: nil)
    }
   func addClickBtn() {
    cancelBtn.addTarget(self, action: #selector(cancelRegisterBtn(sender:)), for: UIControlEvents.touchUpInside)
    sureBtn.addTarget(self, action: #selector(sureLoginBtn(sender:)), for: UIControlEvents.touchUpInside)
    }
    
    //注册
    func  cancelRegisterBtn(sender: UIButton) {

        delegate?.userWillRegister()
    
    }
    //登录
    func  sureLoginBtn(sender: UIButton) {
    
        delegate?.userWillLogin!()
        
    }

    

}
